#ifndef BASE_CTTCPCLIENTTASKPOOL_H_
#define BASE_CTTCPCLIENTTASKPOOL_H_

#include "ctType.h"
#include "ctTCPClientTask.h"
#include "ctTime.h"
#include "ct_noncopyable.h"
#include "ctRWLock.h"
#include "ct_thread_group.h"
#include <list>

class ctCheckconnectThread;
class ctCheckwaitThread;
class ctTCPClientTaskThread;
class ctRecycleThread2;
class ctTCPClientTaskPool : private ctNoncopyable
{
        public:

                ctTCPClientTaskPool()
                        : conn_per_thread_(10)
                {
                        usleep_time_ = 10000;
                        check_wait_thread_ = NULL; 
                }
                ~ctTCPClientTaskPool()
                {                        
                        final();
                }	

                void final();
                bool init();
                void put(ctTCPClientTask **task);
                void timeAction(const ctTime &ct);

                void addCheckwait(ctTCPClientTask *task);
                void addCheckConnect(ctTCPClientTask *task);
                void addRecycle(ctTCPClientTask *task);
                void removeFromCheckConnect(ctTCPClientTask *task);
                bool remove(ctTCPClientTask *task);
                bool addMain(ctTCPClientTask *task);
                void setUsleepTime(int time)
                {
                        usleep_time_ = time;
                }
                bool isReload();
                void resetAll();
                ctTime action_timer_;

        private:
                const DWORD conn_per_thread_;

                ctTCPClientTaskThread *newThread();

                ctCheckconnectThread *check_connect_thread_;
                ctCheckwaitThread *check_wait_thread_;
                ctRecycleThread2 *recycle_thread_;

                ctThreadGroup task_threads_;

                typedef std::list<ctTCPClientTask *> ctTCPClientTaskContainer;
                typedef ctTCPClientTaskContainer::iterator ctTCPClientTask_IT;
                ctTCPClientTaskContainer tasks_;
                ctRWLock rw_lock_;

        public:
                unsigned int usleep_time_; 
};
#endif //BASE_CTTCPCLIENTTASKPOOL_H_
